package com.inforich.app.component;

import com.inforich.app.constant.AuthConstants;
import com.inforich.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 注解解析器（用于解析header中的token->userId->controller注解->接收参数）
 */
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

  @Autowired
  private RedisCache redisCache;

  @Override
  public boolean supportsParameter(MethodParameter parameter) {
    return parameter.getParameterType().isAssignableFrom(LoginUserApp.class) && parameter
        .hasParameterAnnotation(LoginUser.class);
  }

  @Override
  public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
      NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
    return request.getAttribute(AuthConstants.USER_CURRENT, RequestAttributes.SCOPE_REQUEST);
  }
}
